package net.kangyee.eventmanager.commands;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import net.kangyee.eventmanager.EventManager;
import net.kangyee.eventmanager.util.ConfigManager;
import net.kangyee.eventmanager.util.MessageManager;

public class CommandManager implements CommandExecutor {

	// 指令存储位置
	// 1:指令名 2:指令对象
	private Map<String, EventManagerCommand> commands = new ConcurrentHashMap<String, EventManagerCommand>();

	private EventManager plugin;
	private ConfigManager config;

	private String[] messageHelp = { "§f/eventmanager join - 参加活动", "§f/eventmanager quit - 退出活动",
			"§f/eventmanager create <活动名称> <T§7(rue)§f/F§7(alse)§f> [全服消息内容] - 创建活动", "§f/eventmanager stop - 结束活动",
			"§f/eventmanager toggle <on/off> - 开关允许/禁止报名", "§f/eventmanager heal <生命值> [游戏名] - 设置生命值(游戏名留空则给所有玩家设置生命值)",
			"§f/eventmanager food <饥饿度> [游戏名] - 设置饥饿度(游戏名留空则给所有玩家设置饥饿度)",
			"§f/eventmanager kick [游戏名] - 从活动中踢出(游戏名留空则将所有玩家踢出活动)",
			"§f/eventmanager tphere [游戏名] - 将玩家传送到你所在位置(游戏名留空则传送所有玩家)",
			"§f/eventmanager freeze [游戏名] - 使玩家禁止移动(游戏名留空则使所有玩家禁止移动)",
			"§f/eventmanager unfreeze [游戏名] - 使玩家允许移动(游戏名留空则允许所有玩家移动)",
			"§f/eventmanager give <物品id> <数量> [游戏名] - 给予玩家物品(游戏名留空则给予所有玩家物品)",
			"§f/eventmanager cmd <指令> [游戏名] - 给玩家执行指令(以控制台身份执行,指令不加斜杠'/',空格使用下划线'_'代替,玩家变量:%player%,游戏名留空则给所有玩家执行指令)",
			"§f/eventmanager g <消息内容> - 发送活动消息", "§f/eventmanager list - 列出所有报名参加活动的玩家",
			"§f/eventmanager reload - 重载config.yml和message.yml", "§f/eventmanager about - 关于插件" };

	public CommandManager(EventManager plugin, ConfigManager config) {
		this.plugin = plugin;
		this.config = config;
	}

	public void init() {
		EventManagerCommand[] emc = { new CmdCreate(this.plugin, this.config), new CmdCmd(this.plugin), new CmdFreeze(this.plugin),
				new CmdG(this.plugin), new CmdGive(this.plugin), new CmdHeal(this.plugin), new CmdFood(this.plugin), new CmdJoin(this.plugin, this.config),
				new CmdKick(this.plugin), new CmdList(this.plugin), new CmdQuit(this.plugin, this.config), new CmdReload(this.plugin),
				new CmdStop(this.plugin, this.config), new CmdToggle(this.plugin), new CmdTphere(this.plugin),
				new CmdUnfreeze(this.plugin) };

		for (EventManagerCommand localCommand : emc) {
			commands.put(localCommand.getCmdName(), localCommand);
		}
	}

	public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
		if (command.getName().equalsIgnoreCase("eventmanager")) {
			if (args.length == 0 || args[0].equalsIgnoreCase("help") || args[0].equalsIgnoreCase("?")) {
				// 当前页数,从第一条开始
				int curPage;
				// 最大页数,每页显示五条文本
				int maxPage = (messageHelp.length / 5) + 1;
				if (args.length <= 1) {
					curPage = 1;
				} else {
					if (!args[1].matches("\\d+")) {
						sender.sendMessage("§c请输入一个阿拉伯数字");
						return true;
					}
					curPage = Integer.parseInt(args[1]);
					if (curPage > maxPage || curPage <= 0) {
						sender.sendMessage("§c请输入一个正确的页码,最大不可大于" + maxPage + "且不可小于等于0");
						return true;
					}
				}
				// 存放某页的指令
				List<String> helpInfo = new ArrayList<String>();
				// 初始索引
				int sIndex = (curPage - 1) * 5;
				// 结束索引
				int eIndex = (curPage * 5) - 1 < messageHelp.length ? (curPage * 5) : messageHelp.length;
				for (int i = sIndex; i < eIndex; i++) {
					helpInfo.add(messageHelp[i]);
				}
				sender.sendMessage("§f[§a活动管理 - 帮助菜单§f]");
				sender.sendMessage(helpInfo.toArray(new String[helpInfo.size()]));
				sender.sendMessage("§f-----[§6页码:" + curPage + "/" + maxPage + "§f]-----");
				return true;
			}
			if (args[0].equalsIgnoreCase("about")) {
				sender.sendMessage("§f[§a活动管理 - 关于插件§f]");
				sender.sendMessage("§f[§6作者§f]:" + plugin.getDescription().getAuthors());
				sender.sendMessage("§f[§6插件版本§f]:" + plugin.getDescription().getVersion());
				sender.sendMessage("§f[§6联系方式(QQ)§f]:1263817308");
				return true;
			}
			if (args.length >= 1) {
				if (!(sender instanceof Player)) {
					sender.sendMessage("无法以控制台身份执行指令");
					return true;
				}
				dispatchCommand((Player) sender, commands.get(args[0].toLowerCase()), args);
			}
		}
		return true;
	}

	// 指令预处理
	private void dispatchCommand(Player player, EventManagerCommand command, String[] args) {
		// 空指令判断
		if (command == null) {
			MessageManager.sendPNormalMessage(player, "你输入的指令不存在,请使用&c/em help [页码]&f查看帮助");
			return;
		}
		// 权限验证
		if (!player.hasPermission(command.getPermission())) {
			MessageManager.sendNoPermMessage(player, command.getPermission());
			return;
		}
		// 当参数长度不在允许的范围内
		if (args.length < command.getMinArgs() || args.length > command.getMaxArgs()) {
			MessageManager.sendNormalMessage(player, "&c参数填写过" + (args.length < command.getMinArgs() ? "少" : "多"));
			MessageManager.sendNormalMessage(player, "&c正确用法: /eventmanager " + command.getCmdName() + " " + command.getArgs());
			return;
		}
		// 活动状态验证,根据情况返回
		if (plugin.getEventState() != command.getStateRequire()) {
			MessageManager.sendNormalMessage(player, "&c活动需要处于" + plugin.getEventState(command.getStateRequire()) + "状态才可使用此功能");
			return;
		}
		// 玩家报名人数检查
		if (command.getRegPCRequire() && plugin.getRegCount() == 0) {
			MessageManager.sendNormalMessage(player, "&c目前没有玩家报名参加活动");
			return;
		}
		command.onCommand(player, args);
	}

}
